import Taro ,{ Component } from '@tarojs/taro'
import { View } from '@tarojs/components'

import './scoreExchangeList.scss'

import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'
import ExchangeItem from '../../component/ExchangeItem/exchangeItem.js'
import {COMMON_CONST} from "../../utils/const";
import {base_url} from "../../network/config";
import DataManager from "../../utils/DataManager";

let mPageNum = 1;
export default class scoreExchangeList extends Component{

  config = {
    navigationBarTitleText: '积分兑换'
  }

  constructor() {
    super(...arguments)
    this.state = {
      exchangeList: [],
    }
  }

  componentWillMount(){
    Taro.showShareMenu({
      withShareTicket: true
    })
  }

  onPullDownRefresh(){
    mPageNum = 1;
    this.loadExchangeList(mPageNum);
  }

  onReachBottom(){
    mPageNum ++;
    this.loadExchangeList(mPageNum)
  }

  componentDidShow(){
    this.loadExchangeList(mPageNum)
  }

  loadExchangeList (pageNo) {
    let _this = this;
    let url = base_url + 'stateless/integral/integralGoodsList'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        pageNo: pageNo,
        pageSize: COMMON_CONST.LIST_SIZE
      },
      method: 'GET',
      success(res) {
        console.log('----exchangelist---------' + JSON.stringify(res))
        if (!res.data || !res.data.data || !res.data.data.list || res.data.data.list.length < 1){
          if (mPageNum > 1){
            mPageNum --;
          }
        }

        if (!res || !res.data || !res.data.data || !res.data.data.list){
          return
        }

        let exchanges = [];
        if (pageNo == 1){
          exchanges = res.data.data.list;
        } else {
          exchanges = _this.state.list.concat(res.data.data.list)
        }

        _this.setState({
          exchangeList: exchanges
        })
      } ,
      fail: function () {
        if (mPageNum > 1){
          mPageNum --;
        }
      },
      complete:function(){
        Taro.stopPullDownRefresh();
      }
    })
  }

  render(){
    return(
      <View className='score-list-container'>
        <View className='score-list-container-item'>
          {this.state.exchangeList.length > 0 ? this.state.exchangeList.map(scoreItem => {
              return (<ExchangeItem scoreData={scoreItem} canExchange goodsTotalCount={scoreItem.totalCount}></ExchangeItem>)
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
          }
        </View>
      </View>
    )
  }
}

